package hw1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

public class Utils {

    public static String readFileAsString(String filePath) throws java.io.IOException {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
    
    public static void writeStringToFile(String str, String path) throws IOException {
        File file = new File(path);
        PrintWriter writer = new PrintWriter(file);
        StringReader sreader = new StringReader(str);
        BufferedReader reader = new BufferedReader(sreader);
        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
        }
        writer.close();
        reader.close();
    }

}
